package net.sppan.base.dao;

import net.sppan.base.entity.mgEntity.MgAward;
import net.sppan.base.entity.mgEntity.MgCode;
import net.sppan.base.entity.mgEntity.MgUserAward;
import net.sppan.base.entity.mgEntity.MgUserCode;
import net.sppan.base.enumType.AwardEnglishNameType;
import net.sppan.base.enumType.CodeStatusTYpe;
import net.sppan.base.enumType.CodeType;
import net.sppan.base.service.IMgAwardService;
import net.sppan.base.service.IMgCodeService;
import net.sppan.base.service.IMgUserAwardService;
import net.sppan.base.service.IMgUserCodeService;
import org.apache.xmlbeans.impl.regex.Match;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.transaction.Transactional;
import java.util.List;
import java.util.Random;

@Repository
public class EntityManageDaoService {

    @PersistenceContext
    private EntityManager entityManager;

    @Autowired
    private IMgCodeService codeService;


    @Autowired
    private IMgUserCodeService userCodeService;


    @Autowired
    private IMgUserAwardService userAwardService;

    @Autowired
    private IMgAwardService awardService;

    public String randMgCode(String codeStatus,String codeType){

        /*String sql1="SELECT COUNT(*)  FROM mg_code where code_Status='"+codeStatus+"' and code_type='"+codeType+"'";

        Object countCode =  entityManager.createNativeQuery(sql1).getSingleResult();

        String sql2 = "SELECT  FLOOR("+Integer.valueOf(countCode+"")+" * RAND());";

        Object randCode =  entityManager.createNativeQuery(sql2).getSingleResult();

        int randCodeReal = Double.valueOf(randCode+"").intValue();

        randCodeReal = codeType.equalsIgnoreCase(CodeType.Free_one.name())?randCodeReal:(randCodeReal+2000000);

        entityManager.clear();*/


        String codeId = generateCode(codeType);

        return codeId;
    }

    private String generateCode(String codeType) {
        int codeId = 0;
        // 100w-200w
        if(codeType.equalsIgnoreCase(CodeType.Free_one.name())){
             codeId = 1000000+new Random().nextInt(1000000);
        }

        //100w VIP
        if(codeType.equalsIgnoreCase(CodeType.Vip.name())){
             codeId = new Random().nextInt(1000000);
        }

        // >200W
        if(codeType.equalsIgnoreCase(CodeType.Buy_one_send_one.name())){
             codeId = 2000000+new Random().nextInt(50000);
        }

        MgCode mgCode = codeService.find(codeId);
        if(mgCode.getCodeStatus().equals(CodeStatusTYpe.WeiLingQu.name())){
            return mgCode.getCode();
        }else{
            return generateCode(codeType);
        }
    }

    @Transactional
    public void hexiaoCode(MgCode mgCode, MgUserCode mgUserCode){
        userCodeService.update(mgUserCode);
        codeService.update(mgCode);
    }
    @Transactional
    public void addUserAwardInfo(MgAward mgAward, MgUserAward mgUserAward) {
        userAwardService.save(mgUserAward);
        awardService.update(mgAward);
    }

    @Transactional
    public void intiAwardNum() {
        List<MgAward> list = awardService.findAll();
        for (MgAward mgAward:list) {
            mgAward.setNum(AwardEnglishNameType.getNumByName(mgAward.getAwardEnglishName()));
            awardService.save(mgAward);
        }
    }
}
